package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate template;


    @Override
    public void insert(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment findById(String id) {
        Optional<StrategyComment> strategyComment = strategyCommentRepository.findById(id);
        return strategyComment.orElse(null);
    }

    @Override
    public List<StrategyComment> findAll() {
        List<StrategyComment> all = strategyCommentRepository.findAll();
        return all;
    }

    @Override
    public void addComment(StrategyComment strategyComment, UserInfo userInfo) {
        strategyComment.setUserId(userInfo.getId());
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setCreateTime(new Date());
        this.insert(strategyComment);
    }

    @Override
    public Page query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        long count = template.count(query, StrategyComment.class);
        if (count == 0){
            return Page.empty();
        }

        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),Sort.Direction.ASC,"createTime");
        query.with(page);
        List<StrategyComment> list = template.find(query, StrategyComment.class);

        return new PageImpl(list, page, count);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //获取当前这条评论
        StrategyComment strategyComment = this.findById(cid);

        //获取评论的对应list
        List<String> thumbuplist = strategyComment.getThumbuplist();

        //查看是否包含当前登录用户id,如果bu包含,就把点赞量+1,然后把当前用户id添加到集合中
        if (!thumbuplist.contains(uid)){
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
            strategyComment.getThumbuplist().add(uid);
        }else {
            //包含,则点击量-1,把当前用户id弹出这个集合
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
            strategyComment.getThumbuplist().remove(uid);
        }
        //更新
        this.update(strategyComment);

    }


}
